var FollowTableList = {// 医生随访表格初始化
    id: "FollowTableList",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.revisitStatus == 2) {
        return {
            css: {
                "background-color": "#fff1d5",
                "overflow": "hidden",
                "white-space": "nowrap",
                "text-overflow": "ellipsis"
            }
        }
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

FollowTableList.initColumn = function () {//初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '诊所', field: 'arrivalStore', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访人员', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '咨询师', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "待计划";
                } else if (value == 2) {
                    return "<span>" + "回访完成" + "</span>"
                } else if (value == 3) {
                    return "回访未完成"
                } else if (value == 4) {
                    return "回访取消"
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访结果', field: 'revisitContent', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: '', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                var ids = row.id + "," + row.reservationsId + "," + row.customerId + "," + row.revisitTime;
                if (row.revisitStatus == 1) {
                    return '<button title="编辑随访" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="FollowTableList.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-edit"></span></button> ' +
                        '<button title="删除随访" onclick="FollowTableList.deleteAppointment(\'' + row.id + '\',\'' + row.customerName + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>' +
                        '<button title="日志" onclick="FollowTableList.daily(\'' + row.reservationsId + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-th"></span></button>'
                } else {
                    return '<button title="查看随访" style="background: #ffffff; border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="FollowTableList.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-eye-open"></span></button> ' +
                        '<button title="删除随访" onclick="FollowTableList.deleteAppointment(\'' + row.id + '\',\'' + row.customerName + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>' +
                        '<button title="日志" onclick="FollowTableList.daily(\'' + row.reservationsId + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-th"></span></button>'
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        }
    ];
    return columns;
};

FollowTableList.check = function () {// 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        FollowTableList.seItem = selected[0];
        return true;
    }
};

FollowTableList.followRevision = function (ids) {//查看随访
    var index = layer.open({
        type: 2,
        title: '随访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/doctorRevisit/look_doctor_follows/' + ids
    });
    this.layerIndex = index;
};

FollowTableList.daily = function (reservationsId) {//日志
    var index = layer.open({
        type: 2,
        title: '日志',
        area: ['80%', '80%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/doctor_daily/' + reservationsId
    });
    this.layerIndex = index;
};

FollowTableList.deleteAppointment = function (id, customerName) {//删除随访
    if (confirm("患者【" + customerName + "】确认删除回访?")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/delete_revisit",
            type: "post",
            data: {
                id: id
            },
            success: function (data) {
                if (data == -1) {
                    alert("亲，不能取消其他医生的随访！");
                    return;
                } else {
                    Feng.success('操作成功!');
                    window.FollowTableList.search();
                }
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
};

FollowTableList.search = function () {
    FollowTableList.table.refresh();
    $('#FollowTableList').bootstrapTable('resetView', {height: $(window).height() - 105});
};

$(function () {
    $("#dropdownMenu2Ul").on('click', 'li', function () {
        var value = $(this).attr('id');
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/getSfById",
            type: "post",
            data: {
                id: value
            },
            success: function (data) {
                $("#remarks2").val(data.remarks);
                $("#remarks").val(data.remarks)
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    $("#dropdownMenu1Ul").on('click', 'li', function () {
        var value = $(this).attr('id');
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/getJgById",
            type: "post",
            data: {
                id: value
            },
            success: function (data) {
                $("#revisitContent").val(data.remarks)
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    $("#dropdownMenu2Ul").on('click', '.glyphicon-remove', function (e) {
        e.stopPropagation();
        var value = $(this).parent().attr('id');
        if (confirm("是否删除此模板？")) {
            $.ajax({
                url: Feng.ctxPath + "/doctorLooked/deleteSfById",
                type: "post",
                data: {
                    id: value
                },
                success: function (data) {
                    Feng.success("删除成功!");
                    window.location.reload();
                },
                error: function (data) {
                }
            });
        }
    });

    $("#dropdownMenu1Ul").on('click', '.glyphicon-remove', function (e) {
        e.stopPropagation();
        var value = $(this).parent().attr('id');
        if (confirm("是否删除此模板？")) {
            $.ajax({
                url: Feng.ctxPath + "/doctorLooked/deleteJgById",
                type: "post",
                data: {
                    id: value
                },
                success: function (data) {
                    Feng.success("删除成功!");
                    window.location.reload();
                },
                error: function (data) {
                }
            });
        }
    });
});